<?php


class WacGoodsTable extends WacCommonTable
{
    
    public static function getInstance()
    {
        return Doctrine_Core::getTable('WacGoods');
    }

    public function getIdNameHashListByGoodsCategorys($ids=array(), $rowsLimit=500, $orderBy= "t1.id asc")
    {
        $params = array();
        $params['andWhere'][] = "t1.goods_category_id in (".implode(',', $ids).")";
        $params['limit']      = $rowsLimit;
        $params['orderBy']    = $orderBy;
        return $this->getHashList("id", "name", $params);
    }

    /*
     * @params
     *      * array(
     *   "id"=>"", "name"=>"", others..
     * )
     * must required
     *
     * @order
     *    src order ojbect, to register new goods category
     * @return goods obj
     */
    public function register($params=array("id"=>"", "name"=>""), $order=null)
    {
        // handle jar
        if(isset($params["jar_id"]) && isset($params["jar_name"])){
            $tmpParams = array(
                'id' => $params["jar_id"], 'name' => $params["jar_name"],
            );
            $objJar = Doctrine::getTable(WacTable::$jar)->register($tmpParams);
        }

        // handle color
        if(isset($params["color_id"]) && isset($params["color_name"])){
            $tmpParams = array(
                'id' => $params["color_id"], 'name' => $params["color_name"],
            );
            $objColor = Doctrine::getTable(WacTable::$color)->register($tmpParams);
        }
        
        $flagFound = false;
        $existedOne = $this->findOneBy("id", $params["id"]);
        if($existedOne != false)
        {
            if($existedOne->getName() == $params["name"])
            {
                $flagFound = true;
            }
        }

        if($flagFound){
            ;
        }
        else {
            $existedOne = $this->findOneBy("name", $params["name"]);
            if($existedOne != false) {
                $flagFound = true;
            }
        }

        if($flagFound){
            $flagSave = false;
            if(isset($params["color_name"]) && $existedOne->getColorId()!=$objColor->getId()){
                $flagSave = true;
                $existedOne->setColorId($objColor->getId());
            }
            if(isset($params["jar_name"]) && $existedOne->getJarId()!=$objJar->getId()){
                $flagSave = true;
                $existedOne->setJarId($objJar->getId());
            }
            if(isset($params["standard_id"]) && ($existedOne->getStandardId()!=$params["standard_id"])){
                $flagSave = true;
                $existedOne->setStandardId($params["standard_id"]);
            }
            if($flagSave){
                $existedOne->save();
            }

        }
        else {
            $existedOne = $this->create();
            $existedOne->setName($params["name"]);
            if(isset($params["code"])) {$existedOne->setCode($params["code"]);}
            if($order != null){
                switch($order->getTable()->getTableName())
                {
                    case WacTable::getTableName(WacTable::$cottonOrder) :
                        $objGoodsCategory = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("code", WacGoodsCategory::$predefinedClassCodes["classCottonCode"]);
                        break;
                    case WacTable::getTableName(WacTable::$dyeOrder) :
                        $objGoodsCategory = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("code", WacGoodsCategory::$predefinedClassCodes["classDyeCode"]);
                        break;
                    case WacTable::getTableName(WacTable::$weaveOrder) :
                        $objGoodsCategory = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("code", WacGoodsCategory::$predefinedClassCodes["classWeaveCode"]);
                        break;
                    case WacTable::getTableName(WacTable::$cleanUpForm) :
                        $objGoodsCategory = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("code", WacGoodsCategory::$predefinedClassCodes["classCleanUpCode"]);
                        break;
                    case WacTable::getTableName(WacTable::$productionOrder) :
                        $objGoodsCategory = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("code", WacGoodsCategory::$predefinedClassCodes["classFinalClothCode"]);
                        break;
                    default:
                        $objGoodsCategory = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("code", WacGoodsCategory::$predefinedClassCodes["classRoot1Code"]);
                        break;
                }
                $existedOne->setGoodsCategoryId($objGoodsCategory->getId());
            }            
            
            if(isset($params["jar_name"])) {$existedOne->setJarId($objJar->getId());}
            if(isset($params["color_name"])) {$existedOne->setColorId($objColor->getId());}
            if(isset($params["goods_quantity"])) {$existedOne->setGoodsQuantity($params["goods_quantity"]);}
            if(isset($params["standard_id"])) {$existedOne->setStandardId($params["standard_id"]);}
            if(isset($params["axis_id"])) {$existedOne->setAxisId($params["axis_id"]);}
            if(isset($params["axis_time"])) {$existedOne->setAxisTime($params["axis_time"]);}
            if(isset($params["wheelbase"])) {$existedOne->setWheelbase($params["wheelbase"]);}
            if(isset($params["spinner_id"])) {$existedOne->setSpinnerId($params["spinner_id"]);}
            if(isset($params["memo"])) {$existedOne->setMemo($params["memo"]);}

            $existedOne->save();
        }

        return $existedOne;
    }
}